package com.ego.receive;

import com.ego.dubbo.service.ContentDubboService;
import com.ego.pojo.BigAdPojo;
import com.ego.pojo.Content;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ContentReceiveListener {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @DubboReference
    private ContentDubboService contentDubboService;

    @Value("${ego.redis.bigad.key.name}")
    private String bigadKeyName;

    @Value("${ego.bigad.id}")
    private Long bigAdId;

    @RabbitListener(queues = "${ego.rabbitmq.queue.content-insert}")
    public void contentInsertReceive(Content content) {

        //将传递过来的content对象直接转化为bigadpojo，从redis中查询list对象出来
        //删除list最后一条数据，且将新建的bigadpojo对象放入list中的第一个位置，再讲list放回redis中
        BigAdPojo bigAdPojo = new BigAdPojo();
        bigAdPojo.setId(content.getId());
        bigAdPojo.setHref(content.getUrl());
        bigAdPojo.setAlt("");
        bigAdPojo.setSrc(content.getPic());
        bigAdPojo.setWidth(670);
        bigAdPojo.setHeight(240);
        bigAdPojo.setSrcB(content.getPic2());
        bigAdPojo.setWidthB(550);
        bigAdPojo.setHeightB(240);

        List<BigAdPojo> bigAdPojos = (List<BigAdPojo>) redisTemplate.opsForValue().get(bigadKeyName);
        if (bigAdPojos != null) {
            if (bigAdPojos.size() < 6) {
                bigAdPojos.add(0, bigAdPojo);
            } else {
                bigAdPojos.remove(5);
                bigAdPojos.add(0, bigAdPojo);
            }
        }

        redisTemplate.opsForValue().set(bigadKeyName, bigAdPojos);
    }

    @RabbitListener(queues = "${ego.rabbitmq.queue.content-update}")
    public void contentUpdateReceive(Content content) {
        BigAdPojo bigAdPojo = new BigAdPojo();
        bigAdPojo.setId(content.getId());
        bigAdPojo.setHref(content.getUrl());
        bigAdPojo.setAlt("");
        bigAdPojo.setSrc(content.getPic());
        bigAdPojo.setWidth(670);
        bigAdPojo.setHeight(240);
        bigAdPojo.setSrcB(content.getPic2());
        bigAdPojo.setWidthB(550);
        bigAdPojo.setHeightB(240);

        List<BigAdPojo> bigAdPojos = (List<BigAdPojo>) redisTemplate.opsForValue().get(bigadKeyName);
        if (bigAdPojos != null) {
            for (BigAdPojo adPojo : bigAdPojos) {
                //更新的数据在list中
                if (adPojo.getId().equals(bigAdPojo.getId())) {
                    bigAdPojos.remove(adPojo);
                    bigAdPojos.add(0, bigAdPojo);
                } else if (bigAdPojos.size() == 6) {
                    bigAdPojos.remove(5);
                    bigAdPojos.add(0, bigAdPojo);
                }
            }
        }

        redisTemplate.opsForValue().set(bigadKeyName, bigAdPojos);
    }

    @RabbitListener(queues = "${ego.rabbitmq.queue.content-delete}")
    public void contentDeleteReceive(Long[] ids) {
        List<Content> contents = contentDubboService.selectContentByPage(bigAdId, 0, 6);
        List<BigAdPojo> bigAdPojos = new ArrayList<>();
        for (Content content : contents) {
            BigAdPojo bigAdPojo = new BigAdPojo();
            bigAdPojo.setId(content.getId());
            bigAdPojo.setHref(content.getUrl());
            bigAdPojo.setAlt("");
            bigAdPojo.setSrc(content.getPic());
            bigAdPojo.setWidth(670);
            bigAdPojo.setHeight(240);
            bigAdPojo.setSrcB(content.getPic2());
            bigAdPojo.setWidthB(550);
            bigAdPojo.setHeightB(240);

            bigAdPojos.add(bigAdPojo);
        }

        redisTemplate.opsForValue().set(bigadKeyName, bigAdPojos);
    }

}
